/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package helpdiabetesj2me;

import javax.microedition.lcdui.*;

/**
 *
 * @author Johan
 */
public class UnitList extends List implements CommandListener {
    private Command done;
    private Displayable curdisp;
    AddItem localreference;
    
     static String[] str_List_title = {"Kies Eenheid :"};
     
    /** Creates a new instance of UnitList */
    public UnitList() {
        super(str_List_title[HelpDiabetes.languageIndex],List.EXCLUSIVE);
        done = new  Command("Ok",Command.OK,0);
        this.addCommand(done);
        this.setCommandListener(this);
    }

    
      
    //first deletes lal units that may still be there
    // then adds the units to the list    
    // nr_of_units should be 2 or 3. Any other value is not correct. 
    // switch the display to this list
    // AddItem is used here to allow getting the current unit number and to change the value afterwards, and to recall 'additem.selectedItem'
    
    public void SelectUnit(AddItem callingobject, int nr_of_units, String [] unitdescription) {
        deleteAll();
        for (int cntr = 0;cntr < nr_of_units; cntr++) {
            if (unitdescription[cntr].indexOf("=") > -1) {
                append(unitdescription[cntr].substring(0,unitdescription[cntr].indexOf("=")),null);
            }    else
            append(unitdescription[cntr],null);
        }
    
        localreference = callingobject;
        setSelectedIndex(callingobject.get_unit_nubmer_to_add() - 1,true);
        
        curdisp = callingobject;
        HelpDiabetes.midlet.getDisplay().setCurrent(this);
    }
    
    public void commandAction(Command command, Displayable displayable) {
        if (command == done) {
            int localtemp;
            if (localreference.get_rec_to_change() == 0) {
                localtemp = 0;
            } else {
                localtemp = 8;
            }

              int standard_amount = localreference.CalculateAmounts_in_b(getSelectedIndex()+1);
              localreference.ItemSelectedWithoutStoringCurdisp(localreference.get_chosen_record_id(),localtemp,standard_amount == 100 ? "0.0" : (new Float((long)standard_amount)).toString(), localreference.get_rec_to_change(), getSelectedIndex()+1);
              
              HelpDiabetes.midlet.getDisplay().setCurrent(curdisp);
        }
    }
    
}
